<?php

	error_reporting(E_ALL);
	include_once('simple_html_dom.php');

	$html = file_get_html('http://localhost/kooperacija2013/verzije/organogram.html');
 
	if(isset($_POST['sviRedovi'])) {
        $brojredova = $_POST['sviRedovi'];
    } else {
        $brojredova = 3;
    };

    if ($brojredova > 3) {
    	$brojredova = $brojredova - 2;
    	$prvired = $html->find('.organogramnovi tr', 0);
    	$drugired = $html->find('.organogramnovi tr', 1);
    	$trecired = $html->find('.organogramnovi tr', 2);
    	$jedanred = $html->find('.organogramnovi tr', 1)->outertext;
    	$separator = '';
		$redovi = implode($separator, array_fill(0, $brojredova, $jedanred));
	    $drugired->outertext = $redovi;
    }
    

	$data[] = '';
	foreach($html->find('#main2') as $code) {
		$data[] = $code;
	}
    $content = $data[1];
    

	$date = date('d-m-Y-H-i-s');
	$filename = 'datum/'.$date.'.html';
	$fp = fopen("$filename","w");
	fwrite($fp,$content);
	fclose($fp);

	// verzija sa jQuery style i class atributima
	if(isset($_POST['style'])) {
        $content2 = $_POST['style'];
    } else {
        $content2 = 'original-stil';
    };
    $output =  array(
    	'date'=> $date,
        'brojredova'=> $brojredova
    );

	

	$filename2 = 'datum/'.$date.'-stil.html';
	$fp2 = fopen("$filename2","w");
	fwrite($fp2,$content2);
	fclose($fp2);

	//echo $date;
	echo json_encode($output,JSON_FORCE_OBJECT);

?>